#!/usr/bin/env bash
if [[ $# < 1 || $# > 2 ]]; then
    echo "Usage: $0 PHP_VERSION [i386]" 1>&2
    echo "e.g. $0 8.0" 1>&2
    echo "The PHP_VERSION is the version of the php docker image to use" 1>&2
    exit 1
fi
# -x Exit immediately if any command fails
# -e Echo all commands being executed.
# -u fail for undefined variables
set -xeu
PHP_VERSION=$1
DOCKER_ARCHITECTURE=${2:-}
if [[ "$DOCKER_ARCHITECTURE" == i386 ]]; then
    # Run test suites on a 32 bit build of php and confirm they have the expected results.
    PHP_IMAGE="i386/php"
    DOCKER_LABEL="i386-php"
else
    PHP_IMAGE="php"
    DOCKER_LABEL="php"
fi

DOCKER_FILE="tests/docker/Dockerfile"

DOCKER_IMAGE="phan-test-runner:$DOCKER_LABEL"
docker build --network=host --build-arg="PHP_IMAGE=$PHP_IMAGE" --build-arg="PHP_VERSION=$PHP_VERSION" --tag="$DOCKER_IMAGE" -f $DOCKER_FILE .
docker run --rm $DOCKER_IMAGE tests/run_all_tests
